package com.nine.algorithm.leetcode.list;

/**
 * @author nine
 * @version 1.0
 * @description 链表节点类
 * @date 24/5/3 22:44
 */
public class ListNode {

	public int val;
	public ListNode next;

	public ListNode() {
	}

	public ListNode(int val) {
		this.val = val;
	}

	public ListNode(int val, ListNode next) {
		this.val = val;
		this.next = next;
	}

	public static ListNode of(int... vals) {
		ListNode head = null;
		ListNode tail = null;
		for (int val : vals) {
			ListNode node = new ListNode(val);
			if (head == null) {
				head = node;
			}
			else {
				tail.next = node;
			}
			tail = node;
		}
		return head;
	}


	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(64);
		sb.append("[");
		ListNode p = this;
		while (p != null) {
			sb.append(p.val);
			if (p.next != null) {
				sb.append(",");
			}
			p = p.next;
		}
		sb.append("]");
		return sb.toString();
	}
}
